#!/bin/bash

function get-code(){
  if [ "$codeUrl" != "" ]; then
       get-code-zip
  elif  [ "$gitCloneUrl" != "" ]; then
      ops-git-clone
  else
    echo "找不到代码!"
    return 1;
  fi

  if [ ! -d code/$folder ]; then
      echo "﹍﹎﹍﹎﹍﹎﹎﹎"
      echo '找不到目录： code/$folder'
      echo "﹊﹉﹊﹉﹊﹉﹊﹊"
      return 1;
  fi


  ${sudo} rm -rf code/$folder/.npmrc
  ${sudo} rm -rf code/maven.setting.xml


  echo "source main.sh" &&  source main.sh
  sys-init

  if [ -d res ]; then
    echo "合并  res --> code, 保留 code 中相同的文件"
    ${sudo} cp -rf code/. res/
    ${sudo} rm -rf code && ${sudo} mv res code
  fi

}

function get-code-zip(){
  if [ "$codeUrl" == "" ]; then
    return;
  fi

  echo -e "\n"`date "+%Y-%m-%d %H:%M:%S"` "下载源码并解压..."

  echo "wget -q $codeUrl -O  code.zip"
  wget -q $codeUrl -O  code.zip

  if [ $? -ne 0 ]; then
    echo -e "\n"`date "+%Y-%m-%d %H:%M:%S"` 下载源码失败!!!
    return 1;
  else
    echo -e "\n"`date "+%Y-%m-%d %H:%M:%S"` "unzip -q -o  -d ./code code.zip"

    unzip -q -o  -d ./code code.zip

  fi
}


function ops-git-clone(){
    if [ "$gitCloneUrl" == "" ]; then
      return;
    fi

    echo "获取代码 $gitCloneUrl"

    echo "${sudo} docker run --rm  --privileged=true $env_dockerBuildAddHostsParameters \
         -v `getPwdInDockerPath`:/code  -w "/code" \
         $env_bashImageName bash -c 'TZ="Asia/Shanghai" ; source docker-git.sh ' "

    ${sudo} docker run --rm  --privileged=true $env_dockerBuildAddHostsParameters \
    -v `getPwdInDockerPath`:/code  -w "/code" \
    $env_bashImageName bash -c 'TZ="Asia/Shanghai" ; source docker-git.sh '


    if [ $? -ne 0 ]; then
        echo -e "\n"`date "+%Y-%m-%d %H:%M:%S"` '获取代码错误!!!'
        return 1
    else
        echo -e "\n"`date "+%Y-%m-%d %H:%M:%S"` '获取代码成功!'
    fi
}